package Models;

import java.util.ArrayList;
/**
 * @author Thomas
 */
public class SupplierCollection
{
	private ArrayList<Supplier> suppliers;
    private static SupplierCollection instance = new SupplierCollection();

	private SupplierCollection()
	{
		this.suppliers = new ArrayList<Supplier>();
		this.suppliers.add(new Supplier("Tømmermann", "Tømmermann allé", 9400, "Nørresundby", 98178541, "Tommy Tømmermann", 123456));
		this.suppliers.add(new Supplier("Byggemand bobs ultimate supplier", "Testvej", 9870, "Aalborg", 87485210, "Finn Finn Ræveskind", 234567));
		this.suppliers.add(new Supplier("Test Supplier", "Test allé 325", 3850, "Testby", 98478552, "Biver Bengtson", 345678));
	}

    public static SupplierCollection getInstance()
    {
        return SupplierCollection.instance;
    }

	public boolean addSupplier(Supplier newSupplier)
	{
		return this.suppliers.add(newSupplier);
	}

	public ArrayList<Supplier> findSupplier(String name, String address, int zipCode, String city, int phoneNo, String contact, int cvr)
	{
		ArrayList<Supplier> foundSuppliers = new ArrayList<Supplier>();
        for (Supplier supplier : this.suppliers)
        {
			if(supplier.isActivated())
			{
				boolean found = false;
				if (supplier.getName().toLowerCase().contains(name.toLowerCase()))
				{ found = true; }
				else if (supplier.getAddress().toLowerCase().contains(address.toLowerCase()))
				{ found = true; }
				else if (supplier.getZipCode() == zipCode)
				{ found = true; }
				else if (supplier.getCity().toLowerCase().contains(city.toLowerCase()))
				{ found = true; }
				else if (supplier.getPhoneNo() == phoneNo)
				{ found = true; }
				else if (supplier.getContact().toLowerCase().contains(contact.toLowerCase()))
				{ found = true; }
				else if (supplier.getCvr() == cvr)
				{ found = true; }

				if (found == true)
				{
					foundSuppliers.add(supplier);
				}
			}
        }
        return foundSuppliers;
	}

	public Supplier getSupplier(int id)
	{
        for (Supplier supplier : this.suppliers)
        {
            if (supplier.getId() == id)
            {
                return supplier;
            }
        }
        return null;
    }

	public ArrayList<Supplier> getAllSuppliers()
	{
		ArrayList<Supplier> activatedSuppliers = new ArrayList<Supplier>();
		for (Supplier supplier : this.suppliers)
		{
			if(supplier.isActivated())
			{
				activatedSuppliers.add(supplier);
			}
		}
		return activatedSuppliers;
	}

	public void removeSupplier(Supplier supplier)
	{
		supplier.setActivated(false);
	}
}
